import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as common from '../../../utils/common';
import * as compDiviService from '../../../services/AccountDivi';
const daoName = "BaseEvaCalc";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0')
  }
  return value
};
const columns = [
  { title: '经济资本占用(元)', key: 'JJZBZY', dataIndex: 'JJZBZY', width: 100, dispaly: true, canEdit: true },
  { title: '经济资本成本(元)', key: 'JJZBCB', dataIndex: 'JJZBCB', width: 100, dispaly: true, canEdit: true },
  { title: 'EVA(元)', key: 'EVA', dataIndex: 'EVA', width: 100, dispaly: true, canEdit: true },
];
let controls = [
  { name: 'JJZBZY', required: true, type: 'text', label: '经济资本占用(元)', grid: 12 / 24 },
  { name: 'JJZBCB', required: true, type: 'text', label: '经济资本成本(元)', grid: 12 / 24 },
  { name: 'EVA', required: true, type: 'text', label: 'EVA(元)', grid: 12 / 24 }
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class dgevaCalcForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false,
      data: [],
      cpqz: "0",
      khpz: "0",
      zydb: "0",
      bzlx: "0",
      bzrpj: "0",
      qx: "0",
      gm: "0",
      hy: "0"
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20 }
    });
    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  edidOkHandler() {
    const form = this.refs.dgevaCalcForm.getForm()
    form.validateFields((err, values) => {
      if (!err) {
        let t = `${daoName}/create`
        const { keyName, record } = this.props;
        let msg = "数据添加成功！"
        if (record[keyName]) {
          values[keyName] = record[keyName];
          t = `${daoName}/patch`
          msg = "数据修改成功！"
        }
        controls.map((c, i) => {
          if (c.type === 'cascader-select') {
            let vs = values[c.name];
            if (vs.length > 0) {
              values[c.name] = vs[vs.length - 1];
            } else {
              values[c.name] = null;
            }
          }
          if (c.dataType === "number") {
            values[c.name] = Number(values[c.name])
          } else if (c.dataType === "date" || c.dataType === "datetime") {
            values[c.name] = !values[c.name] ? null : new Date(values[c.name])
          } else {
            values[c.name] = values[c.name] === undefined ? null : values[c.name]
          }
          if (c.multiple || c.type === 'checkbox-group') {
            values[c.name] = values[c.name].join(',');
          }
        })
        this.state.dataChange = true;
        this.props.dispatch({
          type: t,
          payload: { daoName, values },
        })
      }
    });
  };
  cpqz(key) {
    this.state.cpqz = key
    let cpqz = key
    let khpz = this.state.khpz
    let zydb = this.state.zydb
    let bzlx = this.state.bzlx
    let bzrpj = this.state.bzrpj
    let qx = this.state.qx
    let gm = this.state.gm
    let hy = this.state.hy
    //console.log(cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy)
    this.props.dispatch({
      type: `${daoName}/getXs`,
      payload: { cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy }
    });
  }
  khpz(key) {
    this.state.khpz = key
    let khpz = key
    let cpqz = this.state.cpqz
    let zydb = this.state.zydb
    let bzlx = this.state.bzlx
    let bzrpj = this.state.bzrpj
    let qx = this.state.qx
    let gm = this.state.gm
    let hy = this.state.hy
    //console.log(cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy)
    this.props.dispatch({
      type: `${daoName}/getXs`,
      payload: { cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy }
    });
  }
  zydb(key) {
    this.state.zydb = key
    let zydb = key
    let cpqz = this.state.cpqz
    let khpz = this.state.khpz
    let bzlx = this.state.bzlx
    let bzrpj = this.state.bzrpj
    let qx = this.state.qx
    let gm = this.state.gm
    let hy = this.state.hy
    //console.log(cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy)
    this.props.dispatch({
      type: `${daoName}/getXs`,
      payload: { cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy }
    });
  }
  bzlx(key) {
    this.state.bzlx = key
    let bzlx = key
    let cpqz = this.state.cpqz
    let khpz = this.state.khpz
    let zydb = this.state.zydb
    let bzrpj = this.state.bzrpj
    if (key !== "1" || key !== "2" || key !== "6") {
      bzrpj = "0"
    }
    let qx = this.state.qx
    let gm = this.state.gm
    let hy = this.state.hy
    this.props.dispatch({
      type: `${daoName}/getXs`,
      payload: { cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy }
    });
  }
  bzrpj(key) {
    this.state.bzrpj = key
    let bzrpj = key
    let cpqz = this.state.cpqz
    let khpz = this.state.khpz
    let zydb = this.state.zydb
    let bzlx = this.state.bzlx
    let qx = this.state.qx
    let gm = this.state.gm
    let hy = this.state.hy
    //console.log(cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy)
    this.props.dispatch({
      type: `${daoName}/getXs`,
      payload: { cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy }
    });
  }
  qx(key) {
    this.state.qx = key
    let qx = key
    let cpqz = this.state.cpqz
    let khpz = this.state.khpz
    let zydb = this.state.zydb
    let bzlx = this.state.bzlx
    let bzrpj = this.state.bzrpj
    let gm = this.state.gm
    let hy = this.state.hy
    //console.log(cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy)
    this.props.dispatch({
      type: `${daoName}/getXs`,
      payload: { cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy }
    });
  }
  gm(key) {
    this.state.gm = key
    let gm = key
    let cpqz = this.state.cpqz
    let khpz = this.state.khpz
    let zydb = this.state.zydb
    let bzlx = this.state.bzlx
    let bzrpj = this.state.bzrpj
    let qx = this.state.qx
    let hy = this.state.hy
    this.props.dispatch({
      type: `${daoName}/getXs`,
      payload: { cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy }
    });
  }
  hy(key) {
    this.state.hy = key
    let hy = key
    let cpqz = this.state.cpqz
    let khpz = this.state.khpz
    let zydb = this.state.zydb
    let bzlx = this.state.bzlx
    let bzrpj = this.state.bzrpj
    let qx = this.state.qx
    let gm = this.state.gm
    this.props.dispatch({
      type: `${daoName}/getXs`,
      payload: { cpqz, khpz, zydb, bzlx, bzrpj, qx, gm, hy }
    });
  }
  find() {
    let { cpqzxs, khpzxs, zydbxs, bzdbxs, qxxs, gmxs, hyxs, je, khlr, bzjbl } = 0
    const form = this.refs.editForm.getForm()
    form.validateFields((err, values) => {
      if (!err) {
        if (values.cpqzxs === undefined) {
          values.cpqzxs = 0;
        }
        if (values.khpzxs === undefined) {
          values.khpzxs = 0;
        }
        if (values.zydbxs === undefined) {
          values.zydbxs = 0;
        }
        if (values.bzdbxs === undefined) {
          values.bzdbxs = 0;
        }
        if (values.qxxs === undefined) {
          values.qxxs = 0;
        }
        if (values.gmxs === undefined) {
          values.gmxs = 0;
        }
        if (values.hyxs === undefined) {
          values.hyxs = 0;
        }
        if (values.je === undefined) {
          values.je = 0;
        }
        if (values.khlr === undefined) {
          values.khlr = 0;
        }
        if (values.bzjbl === undefined) {
          values.bzjbl = 0;
        }
        cpqzxs = Number(values.cpqzxs)
        khpzxs = Number(values.khpzxs)
        zydbxs = Number(values.zydbxs)
        bzdbxs = Number(values.bzdbxs)
        qxxs = Number(values.qxxs)
        gmxs = Number(values.gmxs)
        hyxs = Number(values.hyxs)
        je = Number(values.je)
        khlr = Number(values.khlr)
        bzjbl = Number(values.bzjbl)
      }
    });
    compDiviService.GetEVA({ cpqzxs, khpzxs, zydbxs, bzdbxs, qxxs, gmxs, hyxs, je, khlr, bzjbl }
    ).then(function (data) {
      if (data === 'OK') {
        const form = this.refs.editForm.getForm();
        form.resetFields();
        this.componentDidMount()
      } else {
        return;
      }
    }.bind(this)).catch(function (ex) {
      console.log('请求出错', ex)
    });
  }
  render() {
    let { showEdit, record, permID, popedomTypes } = this.props
    const controls2 = [
      {
        name: 'cpqz', required: false, type: 'select', label: '产品权重', grid: 12 / 24,
        data: [
          { value: '1', label: '公司业务-表内业务-一般贷款'},
          { value: '2', label: '公司业务-表内业务-贸易融资' },
          { value: '3', label: '公司业务-表内业务-各项垫款' },
          { value: '4', label: '公司业务-表内业务-银行承兑汇票贴现' },
          { value: '5', label: '公司业务-表内业务-商业承兑汇票贴现' },
          { value: '6', label: '公司业务-表内业务-买入外币票据' },
          { value: '7', label: '公司业务-表外业务-银行承兑汇票' },
          { value: '8', label: '公司业务-表外业务-融资类保函' },
          { value: '9', label: '公司业务-表外业务-非融资保函及其他担保' },
          { value: '10', label: '公司业务-表外业务-信用证' },
          { value: '11', label: '公司业务-表外业务-贷款承诺' },
        ], layout: { labelSpan: 4, wrapperSpan: 19 }, onChange: this.cpqz.bind(this)
      },
      { name: 'cpqzxs', required: false, type: 'disabled', label: '产品权重系数(%)', grid: 12 / 24, },
      {
        name: 'khpz', required: false, type: 'select', label: '客户评级', grid: 12 / 24,
        data: [
          { value: '1', label: '1' },
          { value: '2', label: '2' },
          { value: '3', label: '3' },
          { value: '4', label: '4' },
          { value: '5', label: '5' },
          { value: '6', label: '6' },
          { value: '7', label: '7' },
          { value: '8', label: '8' },
          { value: '9', label: '9' },
          { value: '10', label: '10' },
          { value: '11', label: '11' },
          { value: '12', label: '12' },
          { value: '13', label: '13' },
          { value: '14', label: '未评级' },
        ], layout: { labelSpan: 4, wrapperSpan: 19 }, onChange: this.khpz.bind(this)
      },
      { name: 'khpzxs', required: false, type: 'disabled', label: '客户评级系数(%)', grid: 12 / 24, },
      {
        name: 'zydb', required: false, type: 'select', label: '抵质押担保', grid: 12 / 24,
        data: [
          { value: '1', label: '质押-金融质押品-现金及其等价物-保证金' },
          { value: '2', label: '质押-金融质押品-现金及其等价物-存单' },
          { value: '3', label: '质押-金融质押品-现金及其等价物-理财产品' },
          { value: '4', label: '质押-金融质押品-现金及其等价物-现汇' },
          { value: '5', label: '质押-金融质押品-票据-中国人民银行票据' },
          { value: '6', label: '质押-金融质押品-票据-商业银行本票/银行汇票' },
          { value: '7', label: '质押-金融质押品-票据-银行承兑汇票' },
          { value: '8', label: '质押-金融质押品-票据-商业承兑汇票' },
          { value: '9', label: '质押-金融质押品-票据-票据池' },
          { value: '10', label: '质押-金融质押品-债券-国债' },
          { value: '11', label: '质押-金融质押品-保单-其他具有现金价值的保险单' },
          { value: '12', label: '质押-金融质押品-股票-基金' },
          { value: '13', label: '质押-金融质押品-贵金属-黄金' },
          { value: '14', label: '质押-应收账款-应收账款-应收账款' },
          { value: '15', label: '质押-应收账款-交易类应收账款-交易类应收账款' },
          { value: '16', label: '质押-应收账款-公路收费权-公路收费权' },
          { value: '17', label: '质押-应收账款-其他收费权-城市基础设施收费权' },
          { value: '18', label: '质押-应收账款-其他收费权-农村电网建设与改造工程电费收费权' },
          { value: '19', label: '质押-应收账款-其他收费权-提供其他公共服务产生的收费权' },
          { value: '20', label: '质押-应收账款-其他收费权-污水处理收费权' },
          { value: '21', label: '质押-其他质押品-出口退税账户-出口退税账户' },
          { value: '22', label: '质押-其他质押品-流动资产-保函' },
          { value: '23', label: '质押-其他质押品-流动资产-仓单' },
          { value: '24', label: '质押-其他质押品-流动资产-提单' },
          { value: '25', label: '质押-其他质押品-无形资产-可转让知识产权的财产权' },
          { value: '26', label: '质押-其他质押品-无追索权保理-无追索权保理' },
          { value: '27', label: '质押-其他质押品-有追索权保理-有追索权保理' },
          { value: '28', label: '质押-其他质押品-其他质押品-其他质押品' },
          { value: '29', label: '质押-缺失-缺失-缺失' },
          { value: '30', label: '抵押-商用房地产和居住用房地产-居住用房地产-居住用房' },
          { value: '31', label: '抵押-商用房地产和居住用房地产-商用房地产-办公用房' },
          { value: '32', label: '抵押-商用房地产和居住用房地产-商用房地产-工业用房' },
          { value: '33', label: '抵押-商用房地产和居住用房地产-商用房地产-商业用房' },
          { value: '34', label: '抵押-商用房地产和居住用房地产-居住用建设用地使用权-商用建设用地使用权' },
          { value: '35', label: '抵押-商用房地产和居住用房地产-土地使用权及在建工程-土地使用权及在建工程' },
          { value: '36', label: '抵押-商用房地产和居住用房地产-房产类在建工程-居住用房产类在建工程' },
          { value: '37', label: '抵押-商用房地产和居住用房地产-房产类在建工程-商用房产类在建工程' },
          { value: '38', label: '抵押-其他抵押品-机器设备-电气设备' },
          { value: '39', label: '抵押-其他抵押品-机器设备-电子产品及通信设备' },
          { value: '40', label: '抵押-其他抵押品-机器设备-通用设备' },
          { value: '41', label: '抵押-其他抵押品-机器设备-专用设备' },
          { value: '42', label: '抵押-其他抵押品-机器设备-机器设备（不能独立设定抵押）' },
          { value: '43', label: '抵押-其他抵押品-交通运输设备-车辆' },
          { value: '44', label: '抵押-其他抵押品-交通运输设备-船舶' },
          { value: '45', label: '抵押-其他抵押品-管网、设备-管网、设备' },
          { value: '46', label: '抵押-其他抵押品-流动资产-存货' },
          { value: '47', label: '抵押-其他抵押品-流动资产-流动资产' },
          { value: '48', label: '抵押-其他抵押品-设施类在建工程-设施类在建工程' },
          { value: '49', label: '抵押-其他抵押品-资源资产-森林资源' },
          { value: '50', label: '抵押-其他抵押品-资源资产-资源资产' },
          { value: '51', label: '抵押-缺失-缺失-缺失' },
        ], layout: { labelSpan: 4, wrapperSpan: 19 },
        onChange: this.zydb.bind(this)
      },
      { name: 'zydbxs', required: false, type: 'disabled', label: '抵质押担保系数(%)', grid: 12 / 24 },
      {
        name: 'bzlx', required: false, type: 'select', label: '担保保证类型', grid: 8 / 24,
        data: [
          { value: '1', label: '专业担保公司担保' },
          { value: '2', label: '一般法人保证' },
          { value: '3', label: '自然人保证' },
          { value: '4', label: '备用信用证保证' },
          { value: '5', label: '银行保函保证' },
          { value: '6', label: '集团母公司担保' },
          { value: '7', label: '市区政府承诺函保证' },
          { value: '8', label: '担保基金保证' },
          { value: '9', label: '其他保证' },
          { value: '10', label: '缺失' },
        ], layout: { labelSpan: 6, wrapperSpan: 18 }, onChange: this.bzlx.bind(this)
      },
      {
        name: 'bzrpj', required: false, type: 'select', label: '保证人评级', grid: 8 / 24,
        data: [
          { value: '1', label: '0-1' },
          { value: '2', label: '0-2' },
          { value: '3', label: '0-3' },
          { value: '4', label: '1' },
          { value: '5', label: '2' },
          { value: '6', label: '3' },
          { value: '7', label: '4' },
          { value: '8', label: '5' },
          { value: '9', label: '6' },
          { value: '10', label: '7' },
          { value: '11', label: '8' },
          { value: '12', label: '9' },
          { value: '13', label: '10' },
          { value: '14', label: '11' },
          { value: '15', label: '12' },
          { value: '16', label: '13' },
          { value: '17', label: '未评级' },
        ], layout: { labelSpan: 5, wrapperSpan: 16 }, onChange: this.bzrpj.bind(this), disabled: false
      },
      { name: 'bzdbxs', required: false, type: 'disabled', label: '保证担保系数(%)', grid: 8 / 24, layout: { labelSpan: 8, wrapperSpan: 13 } },
      {
        name: 'qx', required: false, type: 'select', label: '期限', grid: 12 / 24,
        data: [
          { value: '1', label: '3个月以内（含）' },
          { value: '2', label: '3个月-6个月（含）' },
          { value: '3', label: '6个月-1年（含）' },
          { value: '4', label: '1年-3年（含）' },
          { value: '5', label: '3年-5年（含）' },
          { value: '6', label: '5年-10年（含）' },
          { value: '7', label: '10年以上' },
        ], layout: { labelSpan: 4, wrapperSpan: 19 }, onChange: this.qx.bind(this)
      },
      { name: 'qxxs', required: false, type: 'disabled', label: '期限系数(%)', grid: 12 / 24 },
      {
        name: 'gm', required: false, type: 'select', label: '规模', grid: 12 / 24,
        data: [
          { value: '1', label: '符合标准的小微企业' },
          { value: '2', label: '中小企业（不含小微企业）' },
          { value: '3', label: '一般公司' },
        ], layout: { labelSpan: 4, wrapperSpan: 19 }, onChange: this.gm.bind(this)
      },
      { name: 'gmxs', required: false, type: 'disabled', label: '规模系数(%)', grid: 12 / 24 },
      {
        name: 'hy', required: false, type: 'select', label: '行业', grid: 12 / 24,
        data: [
          { value: '1', label: '黑色金属冶炼和压延加工业' },
          { value: '2', label: '铝冶炼' },
          { value: '3', label: '水泥、石灰和石膏制造' },
          { value: '4', label: '平板玻璃制造' },
          { value: '5', label: '船舶及相关装置制造' },
          { value: '6', label: '房地产业' },
          { value: '7', label: '采矿业' },
          { value: '8', label: '其他' },
        ], layout: { labelSpan: 4, wrapperSpan: 19 }, onChange: this.hy.bind(this)
      },
      { name: 'hyxs', required: false, type: 'disabled', label: '行业系数(%)', grid: 12 / 24 },
      { name: 'je', required: false, type: 'number', label: '金额(元)', grid: 12 / 24, placeholder: '请输入金额...', layout: { labelSpan: 4, wrapperSpan: 19 } },
      { name: 'bzjbl', required: false, type: 'number', label: '保证金比例(%)', grid: 12 / 24, placeholder: '请输入保证金比例...' },
      { name: 'khlr', required: false, type: 'number', label: '考核利润(元)', grid: 12 / 24, placeholder: '请输入考核利润...', layout: { labelSpan: 4, wrapperSpan: 19 } },
      { name: 'BT', type: 'button-group', data: [{ value: '1', label: '计算查询', onClick: this.find.bind(this) }], grid: 24 / 24 },
    ];
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <font style={{ fontSize: '24px' }}>【对公经济资本及EVA计算参数】</font>
          <EditFrom ref="editForm" daoName={daoName} controls={controls2} record={record} />
          <font style={{ fontSize: '24px' }}>【对公经济资本及EVA计算公式】</font>
          <br />
          &nbsp;&nbsp;&nbsp;&nbsp;经济资本占用 = 10.5%*(1-减值准备计提比例0.01)*(1-保证金比例)*金额*系数； <br />
          &nbsp;&nbsp;&nbsp;&nbsp;经济资本成本 = 经济资本占用*10%； <br />
          &nbsp;&nbsp;&nbsp;&nbsp;EVA = (考核利润-金额*(1-减值准备计提比例0.01))*0.75-资本成本 <br />
          <font style={{ fontSize: '24px' }}>【对公经济资本及EVA计算结果】</font>
          <DataTable ref="RoleTable" daoName={daoName} columns={columns} controls={controls} footer={false} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state.BaseEvaCalc;
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record
  }
}
dgevaCalcForm = connect(mapStateToProps)(dgevaCalcForm)
export default dgevaCalcForm;

let render = (props) => React.createElement(dgevaCalcForm, props);
let f = new Factory()
f.set("dgevaCalcForm", render)
